import greenfoot.*;  

/*
 * SandBags minigame
 * 
 * Goal of the game is to close the breach by throwing in sandbags. 
 * The sandbags are dropped by the AirPlane actor on random coordinates
 * The sandbag can be one of three types: 
 * Box - disappears quickly when thrown in the breach ; 
 * smallbag - disappears less quick when thrown in the breach; 
 * Bigbag - disappears slowly when thrown in the breach, but player needs to catch the bigbag when dropped otherwise it will explode;
 * 
 * The player receives a certain amount of points when a sandbag is thrown correctly. The amount op points depends on the bagtype
 * 
 * When the player completes a wall of sandbags containing 3 rows of 4 columns before the time expires the game is won. 
 * 
 * (HINT: Only try throwing when the player stands above the road marking)
 * 
 * @author Jasper Jans
 */
public class SandBags extends MiniGame
{
    private long timeStarted = 0;

    public SandBags()
    {   
        
        super(240, 3);  // 240 seconds to complete level 3
        
        setBackground("Sandbags/background.png");
        
        // The breach 
        Breach breach = new Breach(this);
        addObject(breach, 400, 120);
        
        // The player
        Player player = new Player(this);
        addObject(player, 100, 500);
        
        setPaintOrder(
            Label.class, 
            Airplane.class, 
            Sandbag.class,
            Player.class,
            Water.class,
            Breach.class
            );
        
    }
    
    /*
     * While the game is in GameStatus.PLAYING checks to see if airplane exists. If not, start a timer for a second after which 
     * a new airplane is added to the world and starts it's dropping sequence
     */
    public void act()
    {
        
        if(getGameStatus() == GameStatus.PLAYING)
        {
            if(getObjects(Airplane.class).size() != 0)
            {
                timeStarted = 0;
            }
            else
            {
                if(timeStarted == 0)
                    timeStarted = System.currentTimeMillis();
            }
            
            if(timeStarted != 0)
            {
       
                long currentTime = System.currentTimeMillis();
                long elapsedTime = currentTime - timeStarted;
                
                if(elapsedTime / 1000 >= 1)
                {
                    Airplane airplane = new Airplane();
                    
                    // place the airplane random on the left or right of the screen
                    int startX = Greenfoot.getRandomNumber(1) == 0 ? 0 : 600;   
                    int startY = Greenfoot.getRandomNumber(795) + 2;
                    addObject(airplane, startX, startY);
                    
                    airplane.findDropZone();    // find a place to drop the next sandbag
                }
            }
        }
            
        super.act();
         
    }
    
    
}
